#include "comm.hpp"

int main()
{
    //1.创建管道文件
    if (mkfifo(ipcPath.c_str(), MODE) < 0)
    {
        perror("mafifo");
        exit(1);
    }
    //2.正常的文件操作
    int fd = open(ipcPath.c_str(), O_RDONLY);
    if (fd < 0)
    {
        perror("open");
        exit(2);
    }
    //3.编写正常的通信代码
    char buffer[SIZE];
    while (true)
    {
        memset(buffer, '\0', sizeof(buffer));
        ssize_t s = read(fd, buffer, sizeof(buffer) - 1);
        if (s > 0)
        {
            cout << "client say: " << buffer << endl;
        }
        else if (s == 0)
        {
            //end of file
            cerr << "read end of file, client quit, server quit too" << endl;
            break;
        }
        else
        {
            //read error
            perror("read");
            break;
        }
    }

    //4.关闭文件
    close(fd);
    unlink(ipcPath.c_str());//通信完毕，删除管道文件
    return 0; 
}